﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var sql = "select*from Students";

            var table = DBhelper.GetDataTable(sql);

            dataGridView1.DataSource = table;

            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                string sql = "select*from Students";

                var table = DBhelper.GetDataTable(sql);

                dataGridView1.DataSource = table;

                MessageBox.Show("查询内容不能为空","温馨提示");

            }
            else
            {
                var name = textBox1.Text;
                var sql = string.Format("select*from Students where name like '%{0}%'", name);

                var table = DBhelper.GetDataTable(sql);

                dataGridView1.DataSource = table;

                textBox1.Focus();
                textBox1.SelectAll();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();

            var res = form2.ShowDialog();

            if (res == DialogResult.Yes)
            {
                var sql = "select*from Students";

                var table = DBhelper.GetDataTable(sql);

                dataGridView1.DataSource = table;
            }
            else
            {
                MessageBox.Show("未添加成功","很遗憾");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var id = (int)dataGridView1.SelectedRows[0].Cells["id"].Value;
            var name = (string)dataGridView1.SelectedRows[0].Cells["name"].Value;
            var age = (int)dataGridView1.SelectedRows[0].Cells["age"].Value;
            var sex= (string)dataGridView1.SelectedRows[0].Cells["sex"].Value;
            var score = (double)dataGridView1.SelectedRows[0].Cells["score"].Value;

            Form3 form3 = new Form3(id,name,age,sex,score);

            var grade = form3.ShowDialog();
            if (grade == DialogResult.Yes)
            {
                var sql = "select*from Students";

                var table = DBhelper.GetDataTable(sql);

                dataGridView1.DataSource = table;
            }
            else
            {
                MessageBox.Show("您暂未修改","温馨提示");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var delete = dataGridView1.SelectedRows;

            if (delete.Count > 0)
            {
                var id = (int)delete[0].Cells["id"].Value;
                var sql = string.Format("delete from Students where id={0}",id);

                var count = DBhelper.AddOrUpdateOrdelete(sql);

                if (count > 0)
                {
                    MessageBox.Show("删除成功","提示");
                }
                else
                {
                    MessageBox.Show("您未删除成功","温馨提示");
                }

            }
            else
            {
                MessageBox.Show("请您选择一行要删除的数据","温馨提示");
            }
            this.DialogResult = DialogResult.Yes;
            this.Close();
        }
    }
}
